package com.example.myapplication;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.icu.text.CaseMap;
import android.icu.text.SimpleDateFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class Rateactivity10 extends ListActivity implements AdapterView.OnItemClickListener{
    Handler handler;
    MyAdapter myAdapter;
    private static final String TAG="Rate10";
    private String logDate = "";
    private final String DATE_SP_KEY = "lastRateDateStr";
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        SharedPreferences sp = getSharedPreferences("myrate", Context.MODE_PRIVATE);
        logDate = sp.getString(DATE_SP_KEY, "");
        Log.i("List","lastRateDateStr=" + logDate);


        ArrayList<HashMap<String,String>> listItems = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ItemTitle", "Rate： " + i);
            map.put("ItemDetail", "detail" + i);
            listItems.add(map);}
        SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItems,
                R.layout.list_item,
                new String[] { "ItemTitle", "ItemDetail" },
                new int[] { R.id.itemTitle, R.id.itemDetail }

        );
        setListAdapter(listItemAdapter);
        handler=new Handler(Looper.myLooper()){
            public void handleMessage(@NonNull Message msg){
                if(msg.what==11){
                    Toast.makeText(Rateactivity10.this,"数据已更新",Toast.LENGTH_SHORT).show();
                    ArrayList<HashMap<String,String>> list2 = (ArrayList<HashMap<String, String>>) msg.obj;

                    myAdapter=new MyAdapter(Rateactivity10.this,R.layout.list_item,list2);
                    setListAdapter(myAdapter);

                    RateManager rateManager=new RateManager(Rateactivity10.this);
                    RateItem item =new RateItem();
                    item.setCname("币种");
                    item.setCval(123.4f);
                    rateManager.add(item);
                    Toast.makeText(Rateactivity10.this,"success",Toast.LENGTH_SHORT).show();

                }
                super.handleMessage(msg);

            }


        };
        RateActivity mythread=new RateActivity();
        mythread.setHandler(handler);
        Thread t=new Thread(mythread);
        t.start();

        getListView().setOnItemClickListener(this);
        getListView().setOnItemLongClickListener(this::onItemLongClick);




    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        HashMap<String,String> map=(HashMap<String, String>) getListView().getItemAtPosition(position);
        Log.i(TAG,"OnItemClick:");
        String name=map.get("Title");
        String rate=map.get("detail");
        Log.i(TAG,"onItemClick:name="+name+"==>"+rate);
      //  Intent hh = new Intent(this, rate_ten.class);
      //  hh.putExtra("name",name);
      //  hh.putExtra("rate",rate);
       // startActivity(hh);
        //myAdapter.remove(map);



    }
    public boolean onItemLongClick(AdapterView<?> adapterView,View view,int i,long l){
        Log.i(TAG,"长按成功");
        return true;
    }


}
